﻿using System;
using System.Linq;
using Evpo.ContactData.Data;
using Evpo.ContactData.Common;
using System.Collections.Generic;

namespace Evpo.ContactData.ViewModel
{
    public class AddressEditViewModel : ContactViewModel
    {
        #region Contants
        protected const string DefaultCountry = "Australia";
        #endregion Contants

        public AddressEditViewModel(ModelFactory factory, IConnection conn)
            : base(factory, conn)
        {
            _adapter = factory.CreateAddressAdapter(conn);
        }

        #region Properties

        #endregion Properties


        #region Fields
        private readonly AddressAdapter _adapter;
        #endregion Fields


        #region Methods

        public List<ContactListItem> GetAddressResidents(int addressID)
        {
            List<Contact> residents = _contactAdapter.GetContactsByAddressID(addressID);

            return ContactListViewModel.ContactsToContactListItems(
                residents,
                _titleAdapter);
        }

        public void UpdateContact(int contactID, int addressID)
        {
            Contact contact = _contactAdapter.Load(contactID);
            contact.AddressID = addressID;
            _contactAdapter.Save(contact, isNew: false);
        }

        public void Save(IAddress address, bool isNew)
        {
            _adapter.Save((Address)address, isNew);
        }

        public IAddress Load(int addressID)
        {
            return _adapter.Load(addressID);
        }

        public IAddress CreateNewAddress()
        {
            return new Address()
            {
                Country = DefaultCountry,
            };
        }

        #endregion Methods

    }
}
